package com.fingard.dsp.bank.directbank.jhbank02.utils;

import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class PublickeyEncript {


	private static String certificatePath= "E:\\certificate\\88887_qy.cer";
	/**
	 * ��Կ����
	 * 
	 * @param @param data @param encryptkey @return @throws
	 */
	public static Map<String, String> encryptData(String json) throws Exception {
		System.out.println("json����=============>" + json);

		// ��ȡ��Կ
		PublicKey pubKeyFromCrt = RSA.getPubKeyFromCRT(certificatePath);
		// �������16����
		String key = getRandom(16);
		// ʹ��RSA�㷨���Լ�������ɵ�AESkey����
		String encryptKey = RSA.encrypt(key, pubKeyFromCrt);
		// ʹ��AES�㷨��������ɵ�AESkey����json�����м���
		String encryData = AES.encryptToBase64(json, key);

		System.out.println("����key============>" + encryptKey);
		System.out.println("��������===========>" + encryData);
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("data", encryData);
		map.put("encryptkey", encryptKey);

		return map;
	}
	
	public static Random random = new Random();

	public static String getRandom(int length) {
		StringBuilder ret = new StringBuilder();
		for (int i = 0; i < length; i++) {
			boolean isChar = (random.nextInt(2) % 2 == 0);// �����ĸ��������
			if (isChar) { // �ַ���
				int choice = (random.nextInt(2) % 2 == 0) ? 65 : 97; // ȡ�ô�д��ĸ����Сд��ĸ
				ret.append((char) (choice + random.nextInt(26)));
			} else { // ����
				ret.append(Integer.toString(random.nextInt(10)));
			}
		}
		return ret.toString();
	}
	
	public static void main(String args[]){
		
        try {
        	String reqStr = "��Կ����";
            //����
            System.err.println("��Կ����");
			Map<String,String>  map = PublickeyEncript.encryptData(reqStr);
			System.err.println("��Կ��֤ǩ��");
	        String reqStr2="EJn4jUds9FwU1r7mDkJkJUrIw90ox1nQzUNLZH";
	        //��reqStr2����ǩ����õ�str
	        String str="f4R+u/2IN72O7dmHj5Y7WCEWr9nDklBh7ToFMGJVNs+339274o0iRxFAO8aOlVfXmezpFpOCtUmjUKnXUzKGPILQcKwv1WyxL8TdPm7GFhmY8Wikcl2ciwlk1SZwbyxuQg5uLMod1SC1sATTR29/tYfaNEEBTIQpaMlxsGKEzBF2UPnwg6zKcfNZMeFVGDuoc6pIBWSkNTK34cE111DepOu8EKxOfJyx9mvJHTt2UX8nhroaWtYTxGkTz8A0LwrCtdfqgnQnJ/H/O1GAffFlZMUCb2vhJgfC4je9y6d7W1vm+pBMxPZaVXwmde/XXW7pOl+DfqjAUAqxhIjtd7Qhtw==";
	        boolean b= PublickeyEncript.checkSign(reqStr2, str);
	        System.out.println("ǩ����֤�����"+b);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * �������֤��ǩ��
	 * @param str
	 * @param sign
	 * @return
	 * @throws Exception
	 */
	public static boolean checkSign(String str,String sign) throws Exception{
		PublicKey pubKeyFromCrt = RSA.getPubKeyFromCRT(certificatePath);
		boolean  b=RSA.checkSign(str, sign, pubKeyFromCrt);
		return b;
		
	}
	
}
